package com.example.littleSpider.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.littleSpider.myapplication.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 多用于购物界面的数字更改控件
 * Created by littleSpider on 2017/12/15.
 */

public class ChangeCountView extends RelativeLayout {

    @BindView(R.id.tv_count)
    TextView tv_count;

    int num = 1;
    int maxCount = -1;

    public ChangeCountView(Context context) {
        super(context);
    }

    public ChangeCountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.view_num_choose, this);
        ButterKnife.bind(view);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    /**
     * 设置可选择的最大值
     * @param maxNum
     */
    public void setMax(int maxNum){
     maxCount = maxNum;
    }

    @OnClick(R.id.btn_subtract)
    public void subtract(){
        if (num >= 1){
            num -= 1;
        }
        tv_count.setText(String.valueOf(num));
    }

    @OnClick(R.id.btn_add)
    public void add(){
        if (maxCount == -1^num < maxCount){
            num += 1;
            tv_count.setText(String.valueOf(num));
        }
    }

    /**
     * 获取数字更改器上的数字
     * @return
     */
    public int getNum(){
        return num;
    }
}
